package cn.cvs.controller;

import cn.cvs.pojo.SysRole;
import cn.cvs.pojo.SysUser;
import cn.cvs.service.SysRoleService;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping({"/role"})
public class SysRoleController {
    @Resource
    private SysRoleService sysRoleService;

    @GetMapping(
            value = {"/simpleList"},
            produces = {"application/json;charset=UTF-8"}
    )
    @ResponseBody
    public Object getRoleList() throws Exception {
        List<SysRole> roleList = this.sysRoleService.getRoleList();
        return JSONArray.toJSONString(roleList);
    }

    @RequestMapping({"list"})
    public String getSelectList(Model model,@RequestParam(defaultValue = "1") Integer pageIndex) throws Exception {
        List<SysRole> roleList=this.sysRoleService.getRoleList();
        PageHelper.startPage(pageIndex,1);
        PageInfo<SysRole> pageInfo=new PageInfo<>(roleList);
        model.addAttribute("roleList",roleList);
        model.addAttribute("page",pageInfo);
        return "sysRole/list";
    }

    @GetMapping({"/toAdd"})
    public String addRole() {
        return "sysRole/add";
    }

    @PostMapping({"/add"})
    public String addRoleSave(SysRole sysRole, HttpSession session) throws Exception {
        sysRole.setCreatedUserId(((SysUser)session.getAttribute("userSession")).getId());
        int count=this.sysRoleService.add(sysRole);
        if (count>0)
        {
            return "redirect:/role/list";
        }
        return "sysRole/add";
    }

    @RequestMapping({"/codeExist"})
    @ResponseBody
    public Object odeIsExit(@RequestParam(required = true) String code) throws Exception {
        HashMap<String, Object> resultMap = new HashMap();
        if (code != null && !"".equals(code.trim())) {
            int count = this.sysRoleService.getCountByCode(code);
            if (count == 0) {
                resultMap.put("exist", 0);
            } else {
                resultMap.put("exist", 1);
            }
        } else {
            resultMap.put("exist", -1);
            resultMap.put("msg", "角色编号不可以为空");
        }

        return JSONArray.toJSONString(resultMap);
    }

    @GetMapping({"/toUpdate/{id}"})
    public String getUserById(@PathVariable Integer id, Model model) throws Exception {
        SysRole sysRole = this.sysRoleService.getById(id);
        model.addAttribute(sysRole);
        return "sysRole/update";
    }

    @PostMapping({"/update"})
    public String update(SysRole sysRole, HttpSession session) throws Exception {
        sysRole.setUpdatedUserId(((SysUser)session.getAttribute("userSession")).getId());
        return this.sysRoleService.update(sysRole) ? "redirect:/role/list" : "sysRole/update";
    }

    @DeleteMapping({"/del/{id}"})
    @ResponseBody
    public Object del(@PathVariable(value = "id",required = true) Integer id) throws Exception {
        HashMap resultMap = new HashMap();
        int count=sysRoleService.deleteById(id);
        try {
            if (count>0) {
                resultMap.put("delResult", "true");
            } else {
                resultMap.put("delResult", "false");
            }
        } catch (Exception var4) {
            var4.printStackTrace();
        }
        return JSONArray.toJSONString(resultMap);
    }
}
